<template>
	<view class="flex-col">
		<view class="header">
			<image :src="ImageUrl + '/1/logo.png'" alt="logo" />
			
			<view class="text">
				先登录
			</view>
		</view>
		<view class="flex-row items-center justify-center">
			<view>请完成微信授权以继续使用</view>
			</view>
		<button 
		class="bottom" 
		hover-class="hover-opacity"  
		type="primary" 
		@click="bindGetUserInfo" 
		v-if="show">微信授权登录</button>
		<button 
		class="bottom" 
		type="primary" 
		hover-class="hover-opacity" 
		open-type="getPhoneNumber" 
		lang="zh_CN" 
		@getphonenumber="getPhoneNumber" 
		v-else>进入</button>
		<view class="flex-row items-center justify-center">
			<image @click="isCheck = !isCheck" v-if="isCheck" src="@/static/img/chenggog.png" mode="" style="width:30rpx;height: 26rpx;"></image>
			<image @click="isCheck = !isCheck" v-else src="@/static/img/chenggogn-2@2x.png" mode="" style="width:30rpx;height: 26rpx;"></image>
			<view @click="isCheck = !isCheck" style="margin-left:10upx;font-size: 24upx;">已阅读并同意</view>
			<navigator url="/pages/public/fuwenben?type=5&name=用户协议" class="selym">《用户协议》</navigator>
			<navigator url="/pages/public/fuwenben?type=1&name=隐私政策" class="selym">《隐私政策》</navigator>
		</view>
		<mix-loading v-if="isLoading"></mix-loading>
		<privacy></privacy>
	</view>
</template>

<script>

export default {
	
	data() {
		return {
			uid: '',
			show: true,
			isCheck:false,
			userInfo:{},
			user:{}
		};
	},
	methods: {
		handleInit(){
			wx.getPrivacySetting({
				success(e){
					console.log(e);
				}
			})
		},
		// 手机号授权登录
		async getPhoneNumber(e) {
			// if (e.detail.errMsg == 'getPhoneNumber:ok') {
			// 	const res = await this.$request('user/phone-login', {
			// 		code: e.detail.code,
			// 		openid: uni.getStorageSync('openid')
			// 	}, { showLoading: true });
			// 	if (res.result == 0) {
			// 		uni.setStorageSync('uid', res.data.uid);
			// 		uni.setStorageSync('token', res.data.token);
			// 		uni.setStorageSync('phone', res.data.phone); 
			// 		this.$util.msg('登录成功');
			// 		setTimeout(() => { 
			// 			this.navTo('/pages/tabs/home');
			// 		}, 1000);
			// 	} else {
			// 		this.$util.msg(res.resultNote);
			// 	}
			// }

			// 使用数据库中真实存在的用户数据
			// uni.setStorageSync('uid', '4773e122d9814e94b6b9a52ed41430d1');
			uni.setStorageSync('token', 'test_token'); // token值保持不变
			uni.setStorageSync('phone', '13811111111');
			this.$util.msg('登录成功');
			setTimeout(() => {
				this.switchTab('/pages/tabs/home');
			}, 1000);
		},
		bindGetUserInfo() {
			if(!this.isCheck) return this.$util.msg('请先同意以下协议')
			 wx.getUserProfile({
				 desc: '用于完善用户信息资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				 success:res=>{
					console.log('userInfo',res)
					this.userInfo= res.userInfo,
					this.user.nickname= res.userInfo,
					this.user.headPortrait= res.userInfo,
					wx.setStorageSync('userInfo', res.userInfo)
					const that = this;
					wx.login({
						success (res) {
							if (res.code) {
								let appid='wxe952ea9f90829062'
								let secret='b5a856d637854ec72df94545837e15eb'
								wx.request({
									url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&js_code='+res.code+'&grant_type=authorization_code',
									success:(res)=>{
										this.openid = res.data.openid
										var user= {}
										this.uid = res.data.openid
										that.getLogin(1,res.data.openid).then(ree=>{
											if(ree.result == 0){
												if(ree.data.uid){
													uni.setStorageSync('uid', ree.data.uid);
													this.switchTab('/pages/tabs/home');
												}else{
													uni.setStorageSync('uid', ree.data.uid);
													const data = {
														type: "1",
														thirdId: res.data.openid,
														name:that.userInfo.nickName,
														icon:that.userInfo.avatarUrl,
													}
													that.addUser(data).then(re=>{
														uni.setStorageSync('uid', re.data.uId);
													})
													
												}
											}else{
												
											}
											
										})
										
									}
										
								})
							}
						}
					})
				 }
			 })
			         

			// 直接进行到下一步
			this.show = false;
			// this.uid = '4773e122d9814e94b6b9a52ed41430d1';
		},
		async getLogin(type,thirdId){
			const data = {
				type: type,
				thirdId: thirdId 
			}
			const res = await this.$request('user/thirdLogin', data, {showLoading: true});
			console.log('登录',res )
			return res
		},
		async addUser(data){
			const res = await this.$request('user/addUser', data, {showLoading: true});
			return res
		},
	}
};
</script>
<style>
page {
	background: #ffffff;
}
</style>
<style scoped lang="less">
.selym {
	color: #4e9fc1;
	font-size: 26rpx;
}
.header {
	margin: 80rpx auto 40rpx auto;
	text-align: center;
	.text{
		margin-top: 30rpx;
		text-align: center;
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #222222;
	}
}

.hide {
	opacity: 0;
}

.header image {
	width:180rpx;
	height: 180rpx;
	border-radius: 50%;
	background-color: #FF710B;
	box-shadow: 0 4rpx 24rpx 0 rgba(255,113,11,0.10);
}

.flex-col {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-height: calc(100vh - 100rpx);
	background: #fff;
}
.flex-row {
	display: flex;
	flex-direction: row;
	align-items: center;
}

.bottom {
	width: 591rpx;
	height: 110rpx;
	background: #FF710B !important;
	border-radius: 55rpx;
	font-size: 36rpx;
	font-family: PingFang SC;
	font-weight: bold;
	color: #FFFFFF;
	line-height: 110rpx;
	margin: 60rpx auto 40rpx auto;
	box-shadow: 0 8rpx 32rpx 0 rgba(255,113,11,0.10);
	text-align: center;
}

// 优化提示文字
.flex-row.items-center.justify-center {
	margin: 30rpx 0 0 0;
	font-size: 28rpx;
	color: #333;
	justify-content: center;
}

// 协议勾选icon
.flex-row image {
	margin-right: 8rpx;
	width: 36rpx !important;
	height: 32rpx !important;
}

// 协议文字
.flex-row view {
	font-size: 26rpx;
	color: #666;
}

// 让协议链接更显眼
.selym {
	color: #FF710B;
	font-size: 26rpx;
	margin: 0 4rpx;
}
</style>
